package com.jsmile.cloud.accountcenter.web.mq;

import java.util.Optional;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.jsmile.cloud.accountcenter.web.mq.order.IOrderStatusStrategy;
import com.jsmile.cloud.accountcenter.web.mq.order.OrderStatusChanggeStrategyMap;
import com.jsmile.cloud.tradecenter.api.constants.TradeConstants;
import com.jsmile.cloud.tradecenter.api.enums.OrderStatusEnum;
import com.jsmile.cloud.tradecenter.api.to.OrderStatusChangeTo;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 订单状态变化mq
 *
 * @author 亮亮
 * @version 1.0.0
 * @date 2017年3月30日 下午3:34:16
 */
@Component
@Slf4j
@AllArgsConstructor
public class OrderStatusChangeListener {

    private final OrderStatusChanggeStrategyMap orderStatusChanggeStrategyMap;

    @RabbitListener(queuesToDeclare = @Queue(TradeConstants.TradeMq.ACCOUNT_ORDER_STATUS_SYNC_QUEUE))
    public void handler(OrderStatusChangeTo orderStatusChangeTo) {
        log.info("订单状态变化：{}", JSON.toJSONString(orderStatusChangeTo));
        OrderStatusEnum orderStatus = OrderStatusEnum.nameOf(orderStatusChangeTo.getOrderStatus());
        Optional<IOrderStatusStrategy> syncHandlerOptional = orderStatusChanggeStrategyMap.findStrategyOptional(orderStatus);
        if (syncHandlerOptional.isPresent()) {
            syncHandlerOptional.get().execute(orderStatusChangeTo);
        }
    }
}
